import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;


/**
 * Represents the turn screen. Each turn, most of the actions available
 * to the user will be accessible from here through the use of buttons.
 *
 */
public class TurnPanel extends JPanel implements Serializable {

	private Game game;
	
	private JButton btnMoveButton;
	private JButton btnPaceRations;
	private JButton btnStatus;
	private JButton btnStore;
	private JButton btnMap;
	private JButton btnSave;
	private JLabel distNum;
	private JLabel foodNum;
	transient private JFileChooser fileChooser;

	/**
	 * Creates the turn screen.
	 * 
	 * @param g - the current game.
	 */
	public TurnPanel(Game g) {

		game = g;
		setLayout(null);
		setPreferredSize(new Dimension(800,600));
		
		setBorder(new EmptyBorder(5, 5, 5, 5));
		
		fileChooser = new JFileChooser();
		
		btnMoveButton = new JButton("Move");
		btnMoveButton.setBounds(569, 451, 158, 60);
		add(btnMoveButton);
		btnMoveButton.addActionListener(new ButtonListener());
		
		
		btnPaceRations = new JButton("Pace / Rations");
		btnPaceRations.setBounds(533, 245, 137, 23);
		add(btnPaceRations);
		btnPaceRations.addActionListener(new ButtonListener());
		
		JLabel lblMakeYourMove = new JLabel("Make your move");
		lblMakeYourMove.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lblMakeYourMove.setHorizontalAlignment(SwingConstants.CENTER);
		lblMakeYourMove.setBounds(309, 21, 158, 23);
		add(lblMakeYourMove);
		
		btnStatus = new JButton("Status");
		btnStatus.setBounds(581, 190, 89, 23);
		add(btnStatus);
		btnStatus.addActionListener(new ButtonListener());
		
		btnStore = new JButton("Store");
		btnStore.setBounds(581, 139, 89, 23);
		add(btnStore);
		btnStore.addActionListener(new ButtonListener());
		btnStore.setEnabled(false);

		JLabel lblFoodRemaining = new JLabel("Food Remaining: ");
		lblFoodRemaining.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lblFoodRemaining.setBounds(93, 143, 137, 23);
		add(lblFoodRemaining);
		
		JLabel lblDistanceTraveled = new JLabel("Distance Traveled: ");
		lblDistanceTraveled.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lblDistanceTraveled.setBounds(93, 181, 137, 19);
		add(lblDistanceTraveled);
		
		
		distNum = new JLabel("");
		distNum.setBounds(242, 184, 70, 15);
		add(distNum);
		

		foodNum = new JLabel("");
		foodNum.setBounds(242, 147, 70, 15);
		add(foodNum);
		
		btnMap = new JButton("Map");
		btnMap.setBounds(581, 94, 89, 23);
		btnMap.addActionListener(new ButtonListener());
		add(btnMap);
		
		btnSave = new JButton("Save");
		btnSave.setBounds(93, 470, 89, 23);
		btnSave.addActionListener(new ButtonListener());
		add(btnSave);
	}
	
	/**
	 * Updates the labels on the panel that show remaining food 
	 * and distance traveled.
	 * 
	 * @param distanceTraveled - Number of miles traveled so far
	 * @param foodLeft - Pounds of food remaining.
	 */
	public void updatePanel(int distanceTraveled, int foodLeft, boolean storeAvailable){
		
		distNum.setText(Integer.toString(distanceTraveled));
		foodNum.setText(Integer.toString(foodLeft));
		btnStore.setEnabled(storeAvailable);
	}
	
	//TODO create method to enable or disable store button, every time move is called,
	//		depending on whether at a location where a store is available.
	
	class ButtonListener implements ActionListener
	{

		@Override
		public void actionPerformed(ActionEvent e) 
		{
			if(e.getSource().equals(btnMoveButton))
			{
				game.move();
			}
			else if(e.getSource().equals(btnStatus))
			{
				//TODO Display status screen
			}
			else if(e.getSource().equals(btnPaceRations)) {
				game.changePanel("PaceRationsPanel");
			}
			else if(e.getSource().equals(btnMap)) //Map button!
			{
				game.changePanel("MapPanel");
			}
			else if(e.getSource().equals(btnSave))
			{
				fileChooser.showSaveDialog(game.getTurnPanel());
				game.writeToFile(fileChooser.getSelectedFile().getPath());
			}
			else //source == Store button
			{
				//TODO Display Store screen
				game.changePanel("StorePanel");
			}
		}
	}
}
